package cn.bluethink.timescaledb.util;

import onegis.filter.GFilter;
import org.springframework.util.CollectionUtils;

import java.util.*;

public class DBUtil {

    /**
     * @param columns   数据库中表列集合
     * @param tableKeys 插入数据属性集合
     * @return
     */
    public static Boolean whetherInsert(List<String> columns, Collection<String> tableKeys, Boolean addTimeAndNumber) {

        if (addTimeAndNumber){
            tableKeys.add("time");
            tableKeys.add("number");
        }

        if (tableKeys.size() != columns.size()) {
            return false;
        }

        for (String column : columns) {
            if (!tableKeys.contains(column)){
                return false;
            }
        }
        return true;
    }

    public static String buildWhereSql(GFilter filter) {
        String result = "";
        List<String> whereSqlList = new ArrayList<>();
        whereSqlList.add(buildIdSql(filter));
        //whereSqlList.add(buildGeoWktBox());
        whereSqlList.add(buildStartTimeSql(filter));
        whereSqlList.add(buildEndTimeSql(filter));

        whereSqlList.removeAll(Collections.singleton(""));
        if (whereSqlList.size() > 0) {
            result = String.join(" AND ", whereSqlList);
        }
        return result;
    }
    private static String buildIdSql(GFilter filter){
        String result = "";
        if (!CollectionUtils.isEmpty(filter.getOids())){
            result += (String.format(" oid IN (%s) ", String.join(",", hashSetToList(filter.getOids()))));
        }
        return result;
    }

    /*private String buildGeoWktBox(GFilter filter) throws ParseException {
        String result = "";
        if (filter.getGeoWkt() != null && !"".equals(filter.getGeoWkt())){
            result = String.format(" ST_3DDWithin(geom,st_geomfromewkt('%s'), 0) ", filter.getGeoWkt());
        }
        return result;
    }*/

    private static String buildStartTimeSql(GFilter filter) {
        String result = "";
        if (filter.getStartTime() != null){
            result = String.format(" \"currentTime\" >= %s ", "\'" + filter.getStartTime() + "\'");
        }
        return result;
    }

    private static String buildEndTimeSql(GFilter filter) {
        String result = "";
        if (filter.getEndTime() != null){
            result = String.format(" \"currentTime\" < %s ", "\'" + filter.getEndTime() + "\'");
        }
        return result;
    }

    public static List<String> hashSetToList(HashSet<?> set) {
        List<String> result = new ArrayList<>();
        try {
            if (set.size() > 0) {
                set.forEach(s -> result.add(s.toString()));
            }
        } catch (Exception ex) {
            System.err.println("HashSet转换失败");
        }
        return result;
    }

    public static void main(String[] args) {
        String s1 = "Programming";
        String s2 = new String("Programming");
        String s3 = "Program";
        String s4 = "ming";
        String s5 = "Program" + "ming";
        String s6 = s3 + s4;
        System.out.println(s1 == s2);
        System.out.println(s1 == s5);
        System.out.println(s1 == s6);
        System.out.println(s1 == s6.intern());
        System.out.println(s2 == s2.intern());
    }
}
